#include <func.h>
#define NUM 10000000
int main ()
{
    int shmid = shmget(IPC_PRIVATE,4096,IPC_CREAT|0600);//创建共享内存
    ERROR_CHECK(shmid,-1,"shmget");
    int*p = (int*)shmat(shmid,NULL,0);
    ERROR_CHECK(p,(void*)-1,"shmat");
    p[0] = 0;
    int semid = semget(1000,1,IPC_CREAT|0600);
    ERROR_CHECK(semid,-1,"semget");
    int ret = semctl(semid , 0, SETVAL,1);
    ERROR_CHECK(ret,-1,"semget");
    ret =semctl(semid,0,GETVAL);
    ERROR_CHECK(ret,-1,"semctl GETVAL");
    printf("semval = %d\n",ret);
    struct sembuf P,v;
    P.sem_num = 0 ;//下标；
    P.sem_op = -1;//对资源的影响；
    P.sem_flg = SEM_UNDO;
    v.sem_num = 0;
    v.sem_op = 1;
    v.sem_flg = SEM_UNDO;
    if(fork()==0)
    {
        for(int i = 0;i<NUM;++i)
        {
          semop(semid,&P,1);  
           ++p[0];
           semop(semid,&v,1);
        }
    }
    else
    {
        for(int i = 0;i<NUM;++i)
        {
          semop(semid,&P,1);  
           ++p[0];
           semop(semid,&v,1);

        }
        wait(NULL);
        printf("p[0] = %d\n",p[0]);

    }
    shmdt(p);
    shmctl(shmid,IPC_RMID,NULL);
    }